package com.shuwen.gcdj.service.report;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuwen.gcdj.bean.basicsMaintenance.response.OrderMainResp;
import com.shuwen.gcdj.bean.basicsRepair.response.RepairListResp;
import com.shuwen.gcdj.bean.conditionMonitoring.response.HealthResp;
import com.shuwen.gcdj.bean.equipmentMaintenance.request.EquipmentMaintainReportReq;
import com.shuwen.gcdj.bean.equipmentMaintenance.response.*;
import com.shuwen.gcdj.bean.workingConditions.response.StatisticsDeviceStatusResp;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.entity.StrategyPlandetail;
import com.shuwen.gcdj.entity.basics.BasicsEquipment;
import com.shuwen.gcdj.mapper.mysql.PlanDetailMapper;
import com.shuwen.gcdj.mapper.mysql.Statistic.StatisticsDeviceStatusMapper;
import com.shuwen.gcdj.mapper.mysql.Statistic.StatisticsHealthMapper;
import com.shuwen.gcdj.mapper.mysql.basics.BasicsEquipmentMapper;
import com.shuwen.gcdj.mapper.mysql.basics.OrderMaintenanceorderMapper;
import com.shuwen.gcdj.mapper.mysql.basics.OrderRepairworkorderMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.stream.Collectors;

/**
 * 区域运维报表
 *
 * @author XYC
 */
@Service
@Slf4j
@AllArgsConstructor
public class EquipmentMainTainReportService {

    private final BasicsEquipmentMapper basicsEquipmentMapper;

    private final StatisticsDeviceStatusMapper statisticsDeviceStatusMapper;

    private final OrderRepairworkorderMapper orderRepairworkorderMap;

    private final StatisticsHealthMapper statisticsHealthMapper;

    private final OrderMaintenanceorderMapper orderMaintenanceOrderMapper;

    private final PlanDetailMapper planDetailMappers;


    /**
     * 获取设备运维数据
     *
     * @param req
     * @return
     */
    public CommonResult<EquipmentMaintainReportResp> getEquipmentMaintainReportData(EquipmentMaintainReportReq req) {
        EquipmentMaintainReportResp equipmentMaintainReportResp = new EquipmentMaintainReportResp();
        LocalDateTime startDate = req.getStartDate() != null ? req.getStartDate() : LocalDateTime.now();
        LocalDateTime endDate = req.getEndDate() != null ? req.getEndDate().plusDays(1).minusSeconds(1) : LocalDateTime.now().plusDays(1).minusSeconds(1);
        req.setStartDate(startDate);
        req.setEndDate(endDate);
        List<BasicsEquipment> basicsEquipments;
        if (req.getAreaLocation() != 0) {
            basicsEquipments = basicsEquipmentMapper.selectList(new QueryWrapper<BasicsEquipment>().lambda()
                    .eq(BasicsEquipment::getAreaLocation, req.getAreaLocation())
                    .eq(BasicsEquipment::getIsDelete, Constants.IS_DELETE_NORMAL));
        } else {
            basicsEquipments = basicsEquipmentMapper.selectList(new QueryWrapper<BasicsEquipment>().lambda()
                    .eq(BasicsEquipment::getIsDelete, Constants.IS_DELETE_NORMAL));
        }
        if (basicsEquipments.isEmpty()) {
            return CommonResult.success(equipmentMaintainReportResp);
        }
        List<String> codes = basicsEquipments.stream().map(BasicsEquipment::getCode).collect(Collectors.toList());
        List<String> ids = basicsEquipments.stream().map(BasicsEquipment::getId).collect(Collectors.toList());
        req.setEquipmentCodes(codes);
        //设备状态-运行时长
        List<StatisticsDeviceStatusResp> devicestatus = statisticsDeviceStatusMapper.getListResult(req);
        //健康度
        List<HealthResp> healths = statisticsHealthMapper.getStatistic(req);
        //报修
        List<RepairListResp> repairList = orderRepairworkorderMap.getList(req);
        //预警单
        List<RepairListResp> repairYJ = repairList.stream().filter(repairListResp -> !repairListResp.getOrderCode().isEmpty() && repairListResp.getOrderCode().contains("YJ")).collect(Collectors.toList());
        //维保
        List<OrderMainResp> maintainOrder = orderMaintenanceOrderMapper.getStatistic(req, ids);
        //计划
/*        List<StrategyPlandetail> planDetail = planDetailMappers.getStatistic(req, ids);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");*/
        switch (req.getReportType()) {
            //日报
            case 1: {
                equipmentMaintainReportResp = daily(devicestatus, healths, repairYJ, req, repairList, basicsEquipments);
                break;
            }
            //周报
            case 2: {
                equipmentMaintainReportResp = Weekly(devicestatus, healths, repairYJ, req, repairList, basicsEquipments);
                break;
            }
            //月报
            case 3: {
                equipmentMaintainReportResp = Monthly(devicestatus, healths, repairYJ, req, repairList, basicsEquipments);
                break;
            }
        }
        //工单统计
        equipmentMaintainReportResp.setWorkOrder(getWorkOrderStatisticsData(req));
        //计划统计
        equipmentMaintainReportResp.setPlan(getPlanStatisticsData(req));
        return CommonResult.success(equipmentMaintainReportResp);
    }

    //日报
    public EquipmentMaintainReportResp daily(List<StatisticsDeviceStatusResp> devicestatus, List<HealthResp> healths, List<RepairListResp> repairYJ, EquipmentMaintainReportReq req,
    List<RepairListResp> repairList, List<BasicsEquipment> basicsEquipments)
    {
        EquipmentMaintainReportResp equipmentMaintainReportResp = new EquipmentMaintainReportResp();
        EquipmentMaintainReportReq equipmentMaintainReportReq = new EquipmentMaintainReportReq();
        equipmentMaintainReportReq.setStartDate(req.getStartDate().minusDays(1));
        equipmentMaintainReportReq.setEndDate(req.getEndDate().minusDays(1));
        equipmentMaintainReportReq.setReportType(req.getReportType());
        equipmentMaintainReportReq.setEquipmentCodes(req.getEquipmentCodes());
        equipmentMaintainReportReq.setAreaLocation(req.getAreaLocation());
        //设备状态-运行时长
        List<StatisticsDeviceStatusResp> devicestatusLast = statisticsDeviceStatusMapper.getListResult(equipmentMaintainReportReq);
        //报修
        //List<RepairListResp> repairListLast = orderRepairworkorderMap.getList(equipmentMaintainReportReq);
        //健康度
        List<HealthResp> healthsLast = statisticsHealthMapper.getStatistic(equipmentMaintainReportReq);
        //预警单
        List<RepairListResp> repairYJLast = repairList.stream().filter(listResp -> !listResp.getOrderCode().isEmpty() && listResp.getOrderCode().contains("YJ")).collect(Collectors.toList());
        List<EquipmentGuaranteeResp> guarantees = new ArrayList<>();
        List<EquipmentHealthDegreeResp> healthDegres = new ArrayList<>();
        List<EquipmentExceptionResp> exceptions = new ArrayList<>();
        List<EquipmentBaseResp> details = new ArrayList<>();
        for (BasicsEquipment equipment : basicsEquipments) {
            EquipmentGuaranteeResp guarantee = new EquipmentGuaranteeResp();
            EquipmentHealthDegreeResp healthDegre = new EquipmentHealthDegreeResp();
            EquipmentExceptionResp exception = new EquipmentExceptionResp();
            EquipmentBaseResp detail = new EquipmentBaseResp();
            detail.setCode(equipment.getCode());
            detail.setName(equipment.getName());
            details.add(detail);
            //运行时长
            Float dayTime = devicestatus.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(x -> x.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            Float dayTimeLast = devicestatusLast.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(x -> x.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            dayTimeLast = dayTimeLast == null ? 0 : dayTimeLast;
            dayTime = dayTime == null ? 0 : dayTime;
            float time = dayTime - dayTimeLast;
            String rate = String.format("%.2f", time == 0 ? 0 : (time / 24) * 100);
            guarantee.setName(equipment.getName());
            guarantee.setCode(equipment.getCode());
            guarantee.setTime(Float.toString(time));
            guarantee.setRate(rate + "%");
            guarantees.add(guarantee);
            //健康度
            Float health = healths.stream()
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .max(Comparator.comparing(HealthResp::getTime).reversed())
                    .orElse(new HealthResp())
                    .getStatusValue();
            Float healthLast = healthsLast.stream()
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .max(Comparator.comparing(HealthResp::getTime).reversed())
                    .orElse(new HealthResp())
                    .getStatusValue();
            health = health == null ? 0 : health;
            healthLast = healthLast == null ? 0 : healthLast;
            String degre = String.format("%.2f", health - healthLast);
            healthDegre.setName(equipment.getName());
            healthDegre.setCode(equipment.getCode());
            healthDegre.setValue(String.valueOf(Math.round(health)));
            healthDegre.setTrend(degre + "%");
            healthDegres.add(healthDegre);
            //异常盘点
            Integer yjValue = Math.toIntExact(repairYJ.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            Integer yjValueLast = Math.toIntExact(repairYJLast.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            Integer yjDiff = yjValue - yjValueLast;
            String trend = yjDiff > 0 ? "+" + yjDiff : yjDiff.toString();
            exception.setName(equipment.getName());
            exception.setCode(equipment.getCode());
            exception.setTotal(yjValue.toString());
            exception.setTrend(trend);
            exceptions.add(exception);
        }
        equipmentMaintainReportResp.setGuarantee(guarantees);
        equipmentMaintainReportResp.setHealthDegre(healthDegres);
        equipmentMaintainReportResp.setException(exceptions);
        equipmentMaintainReportResp.setDetail(details);
        return equipmentMaintainReportResp;
    }

    //周报
    public EquipmentMaintainReportResp Weekly(List<StatisticsDeviceStatusResp> devicestatus,
      List<HealthResp> healths, List<RepairListResp> repairYJ, EquipmentMaintainReportReq req,
      List<RepairListResp> repairList, List<BasicsEquipment> basicsEquipments)
    {
        EquipmentMaintainReportResp equipmentMaintainReportResp = new EquipmentMaintainReportResp();
        EquipmentMaintainReportReq equipmentMaintainReportReq = new EquipmentMaintainReportReq();
        equipmentMaintainReportReq.setStartDate(req.getStartDate().minusDays(1));
        equipmentMaintainReportReq.setEndDate(req.getEndDate().minusDays(1));
        equipmentMaintainReportReq.setReportType(req.getReportType());
        equipmentMaintainReportReq.setEquipmentCodes(req.getEquipmentCodes());
        equipmentMaintainReportReq.setAreaLocation(req.getAreaLocation());
        //设备状态-运行时长
        List<StatisticsDeviceStatusResp> devicestatusLast = statisticsDeviceStatusMapper.getListResult(equipmentMaintainReportReq);
        //报修
        //List<RepairListResp> repairListLast = orderRepairworkorderMap.getList(equipmentMaintainReportReq);
        //健康度
        List<HealthResp> healthsLast = statisticsHealthMapper.getStatistic(equipmentMaintainReportReq);
        //预警单
        List<RepairListResp> repairYJLast = repairList.stream().filter(repairListResp -> !repairListResp.getOrderCode().isEmpty() && repairListResp.getOrderCode().contains("YJ")).collect(Collectors.toList());
        List<EquipmentGuaranteeResp> guarantees = new ArrayList<>();
        List<EquipmentHealthDegreeResp> healthDegres = new ArrayList<>();
        List<EquipmentExceptionResp> exceptions = new ArrayList<>();
        List<EquipmentBaseResp> details = new ArrayList<>();
        for (BasicsEquipment equipment : basicsEquipments) {
            EquipmentGuaranteeResp guarantee = new EquipmentGuaranteeResp();
            EquipmentHealthDegreeResp healthDegre = new EquipmentHealthDegreeResp();
            EquipmentExceptionResp exception = new EquipmentExceptionResp();
            EquipmentBaseResp detail = new EquipmentBaseResp();
            detail.setCode(equipment.getCode());
            detail.setName(equipment.getName());
            details.add(detail);
            //运行时长
            Float dayTime = devicestatus.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(deviceStatusResp -> deviceStatusResp.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            Float dayTimeLast = devicestatusLast.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(deviceStatusResp -> deviceStatusResp.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            dayTimeLast = dayTimeLast == null ? 0 : dayTimeLast;
            dayTime = dayTime == null ? 0 : dayTime;
            float time = dayTime - dayTimeLast;
            String.format("%.2f", time == 0 ? 0 : time / 24);
            String rate = String.format("%.2f", time == 0 ? 0 : (time / 24) * 100);
            guarantee.setName(equipment.getName());
            guarantee.setCode(equipment.getCode());
            guarantee.setTime(Float.toString(time));
            guarantee.setRate(rate + "%");
            guarantees.add(guarantee);
            //健康度
            Float health = healths.stream().sorted(Comparator.comparing(HealthResp::getTime).reversed())
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .findFirst().orElse(new HealthResp())
                    .getStatusValue();
            Float healthLast = healthsLast.stream().sorted(Comparator.comparing(HealthResp::getTime).reversed())
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .findFirst().orElse(new HealthResp())
                    .getStatusValue();
            health = health == null ? 0 : health;
            healthLast = healthLast == null ? 0 : healthLast;
            Double degre = (double) Math.round(health - healthLast);
            healthDegre.setName(equipment.getName());
            healthDegre.setCode(equipment.getCode());
            healthDegre.setValue(String.valueOf(Math.round(health)));
            healthDegre.setTrend(degre + "%");
            healthDegres.add(healthDegre);
            //异常盘点
            Integer yjValue = Math.toIntExact(repairYJ.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            Integer yjValueLast = Math.toIntExact(repairYJLast.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            int yjDiff = yjValue - yjValueLast;
            String trend = yjDiff > 0 ? "+" + yjDiff : Integer.toString(yjDiff);
            exception.setName(equipment.getName());
            exception.setCode(equipment.getCode());
            exception.setTotal(yjValue.toString());
            exception.setTrend(trend);
            exceptions.add(exception);
        }
        equipmentMaintainReportResp.setGuarantee(guarantees);
        equipmentMaintainReportResp.setHealthDegre(healthDegres);
        equipmentMaintainReportResp.setException(exceptions);
        equipmentMaintainReportResp.setDetail(details);
        return equipmentMaintainReportResp;
    }

    //月报
    public EquipmentMaintainReportResp Monthly(List<StatisticsDeviceStatusResp> devicestatus,
    List<HealthResp> healths, List<RepairListResp> repairYJ, EquipmentMaintainReportReq req,
    List<RepairListResp> repairList, List<BasicsEquipment> basicsEquipments)
    {
        EquipmentMaintainReportResp result = new EquipmentMaintainReportResp();
        EquipmentMaintainReportReq maint = new EquipmentMaintainReportReq();
        maint.setEndDate(req.getStartDate().minusSeconds(1));
        maint.setStartDate(req.getStartDate().minusMonths(1));
        maint.setReportType(req.getReportType());
        maint.setEquipmentCodes(req.getEquipmentCodes());
        maint.setAreaLocation(req.getAreaLocation());
        //时差
        //Integer diffHour = Math.toIntExact(ChronoUnit.DAYS.between(maint.getStartDate(), maint.getEndDate()) * 24);

        //设备状态-运行时长
        List<StatisticsDeviceStatusResp> devicestatusLast = statisticsDeviceStatusMapper.getListResult(maint);
        //报修
        //List<RepairListResp> repairListLast = orderRepairworkorderMap.getList(maint);

        //健康度
        List<HealthResp> healthsLast = statisticsHealthMapper.getStatistic(maint);

        //预警单
        List<RepairListResp> repairYJLast = repairList.stream().filter(repairListResp -> !repairListResp.getOrderCode().isEmpty() && repairListResp.getOrderCode().contains("YJ")).collect(Collectors.toList());

        List<EquipmentGuaranteeResp> guarantees = new ArrayList<>();
        List<EquipmentHealthDegreeResp> healthDegres = new ArrayList<>();
        List<EquipmentExceptionResp> exceptions = new ArrayList<>();
        List<EquipmentBaseResp> details = new ArrayList<>();
        for (BasicsEquipment equipment : basicsEquipments) {

            EquipmentGuaranteeResp guarantee = new EquipmentGuaranteeResp();
            EquipmentHealthDegreeResp healthDegre = new EquipmentHealthDegreeResp();
            EquipmentExceptionResp exception = new EquipmentExceptionResp();
            EquipmentBaseResp detail = new EquipmentBaseResp();
            detail.setCode(equipment.getCode());
            detail.setName(equipment.getName());
            details.add(detail);
            //运行时长
            Float dayTime = devicestatus.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(deviceStatusResp -> deviceStatusResp.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            Float dayTimeLast = devicestatusLast.stream().sorted(Comparator.comparing(StatisticsDeviceStatusResp::getTime).reversed())
                    .filter(deviceStatusResp -> deviceStatusResp.getEquipmentCode().equals(equipment.getCode()))
                    .findFirst().orElse(new StatisticsDeviceStatusResp())
                    .getCumulativeUmulativeuntimeDuration();
            dayTimeLast = dayTimeLast == null ? 0 : dayTimeLast;
            dayTime = dayTime == null ? 0 : dayTime;
            float time = dayTime - dayTimeLast;
            String rate = String.format("%.2f", time == 0 ? 0 : (time / (24 * 7)) * 100);
            guarantee.setName(equipment.getName());
            guarantee.setCode(equipment.getCode());
            guarantee.setTime(Float.toString(time));
            guarantee.setRate(rate + "%");
            guarantees.add(guarantee);
            //健康度
            Float health = healths.stream().sorted(Comparator.comparing(HealthResp::getTime).reversed())
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .findFirst().orElse(new HealthResp())
                    .getStatusValue();
            Float healthLast = healthsLast.stream().sorted(Comparator.comparing(HealthResp::getTime).reversed())
                    .filter(healthResp -> healthResp.getEquipmentCode().equals(equipment.getCode()) && healthResp.getLocation().equals(1))
                    .findFirst().orElse(new HealthResp())
                    .getStatusValue();
            health = health == null ? 0 : health;
            healthLast = healthLast == null ? 0 : healthLast;
            double degre = Math.round(health - healthLast);
            healthDegre.setName(equipment.getName());
            healthDegre.setCode(equipment.getCode());
            healthDegre.setValue(String.valueOf(Math.round(health)));
            healthDegre.setTrend(degre + "%");
            healthDegres.add(healthDegre);
            //异常盘点
            Integer yjValue = Math.toIntExact(repairYJ.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            Integer yjValueLast = Math.toIntExact(repairYJLast.stream().filter(repairListResp -> repairListResp.getEquipmentNo().equals(equipment.getCode())).count());
            int yjDiff = yjValue - yjValueLast;
            String trend = yjDiff > 0 ? "+" + yjDiff : Integer.toString(yjDiff);
            exception.setName(equipment.getName());
            exception.setCode(equipment.getCode());
            exception.setTotal(yjValue.toString());
            exception.setTrend(trend);
            exceptions.add(exception);
        }
        result.setGuarantee(guarantees);
        result.setHealthDegre(healthDegres);
        result.setException(exceptions);
        result.setDetail(details);
        return result;
    }

    public WorkOrderStatisticsResp getWorkOrderStatisticsData(EquipmentMaintainReportReq req) {
        WorkOrderStatisticsResp workOrderStatisticsResp = new WorkOrderStatisticsResp();
        RepairOrderStatisticsResp repairOrderStatisticsResp = new RepairOrderStatisticsResp();
        MaintainOrderStatisticsResp maintainOrderStatisticsResp = new MaintainOrderStatisticsResp();
        List<BasicsEquipment> basicsEquipments = basicsEquipmentMapper.selectList(new QueryWrapper<BasicsEquipment>().lambda());
        basicsEquipments = basicsEquipments.stream().filter(x -> req.getEquipmentCodes().contains(x.getCode())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(basicsEquipments)) {
            return workOrderStatisticsResp;
        }
        //List<String> codes = basicsEquipments.stream().map(BasicsEquipment::getCode).collect(Collectors.toList());
        List<String> ids = basicsEquipments.stream().map(BasicsEquipment::getId).collect(Collectors.toList());
        //报修
        List<RepairListResp> repairList = orderRepairworkorderMap.getList(req);
        //预警单
        List<RepairListResp> repairYJ = repairList.stream().filter(repairListResp -> !repairListResp.getOrderCode().isEmpty() && repairListResp.getOrderCode().contains("YJ")).collect(Collectors.toList());
        //维保
        List<OrderMainResp> maintainOrder = orderMaintenanceOrderMapper.getStatistic(req, ids);
        //工单总数
        workOrderStatisticsResp.setTotal(Math.toIntExact((long) maintainOrder.size() + (long) repairList.size()));
        repairOrderStatisticsResp.setTotal(Math.toIntExact(repairList.size()));
        repairOrderStatisticsResp.setFinishd(Math.toIntExact(repairList.stream().filter(repairListResp -> repairListResp.getState().equals(6)).count()));
        repairOrderStatisticsResp.setAI(Math.toIntExact(repairYJ.size()));
        repairOrderStatisticsResp.setLabor(repairOrderStatisticsResp.getTotal() - repairOrderStatisticsResp.getAI());
        workOrderStatisticsResp.setRepairOrder(repairOrderStatisticsResp);
        maintainOrderStatisticsResp.setTotal(Math.toIntExact(maintainOrder.size()));
        maintainOrderStatisticsResp.setFinishd(Math.toIntExact(maintainOrder.stream().filter(orderMainResp -> orderMainResp.getState().equals(1)).count()));
        maintainOrderStatisticsResp.setAddOrderTotal(Math.toIntExact(maintainOrder.size()));
        workOrderStatisticsResp.setMaintainOrder(maintainOrderStatisticsResp);
        return workOrderStatisticsResp;
    }


    public List<EquipmentPlanResp> getPlanStatisticsData(EquipmentMaintainReportReq req) {
        List<EquipmentPlanResp> equipmentPlanResps = new ArrayList();
        List<BasicsEquipment> basicsEquipments = basicsEquipmentMapper.selectList(new QueryWrapper<BasicsEquipment>().lambda());
        basicsEquipments = basicsEquipments.stream().filter(x -> req.getEquipmentCodes().contains(x.getCode())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(basicsEquipments)) {
            return equipmentPlanResps;
        }
        //List<String> codes = basicsEquipments.stream().map(BasicsEquipment::getCode).collect(Collectors.toList());
        List<String> ids = basicsEquipments.stream().map(BasicsEquipment::getId).collect(Collectors.toList());
        //计划
        List<StrategyPlandetail> planDetail = planDetailMappers.getStatistic(req, ids);
        planDetail = planDetail.stream().filter(x -> x.getMaintenanceType().equals(2)).collect(Collectors.toList());
        for (BasicsEquipment equals : basicsEquipments) {
            EquipmentPlanResp data = new EquipmentPlanResp();
            Integer count = Math.toIntExact(planDetail.stream().filter(x -> x.getEquipmentId().equals(equals.getId())).count());
            data.setName(equals.getName());
            data.setCode(equals.getCode());
            data.setSecondTotal(count.toString());
            equipmentPlanResps.add(data);
        }
        return equipmentPlanResps;
    }

}
